package cn.mumzone.pos.ui.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.mumzone.basic.util.ListUtil;
import cn.mumzone.basic.util.StringUtils;
import cn.mumzone.pos.R;
import cn.mumzone.pos.model.ServiceEntity;

/**
 * Created by qiuzhenhuan on 2019/6/14.
 */

public class ADA_AddServiceList extends RecyclerView.Adapter<ADA_AddServiceList.ViewHolder> {

    private Context mContext;

    private List<ServiceEntity> mData;

    private AddServeListener listener;

    private boolean isMember = false;

    public ADA_AddServiceList(Context context) {
        this.mContext = context;
    }

    public void setIsMember(boolean isMember) {
        this.isMember = isMember;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = View.inflate(mContext, R.layout.item_service_list, null);
        return new ViewHolder(view);
    }


    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        final ServiceEntity serviceEntity = mData.get(position);

        holder.tvServiceName.setText(StringUtils.getString(serviceEntity.getServiceName()));
        holder.tvServiceTime.setText(String.format("时长：%s分钟", StringUtils.getString(serviceEntity.getServiceTime())));
        holder.tvServicePrice.setText(StringUtils.getFormattedMoneyWithUnit(isMember ? serviceEntity.getMemberPrice() : serviceEntity.getPrice()));
        holder.tvCardTag.setVisibility(!TextUtils.isEmpty(serviceEntity.getCard()) &&
                serviceEntity.getCard().equals("1") ? View.VISIBLE : View.GONE);
        holder.tvSelectCount.setText(StringUtils.getString(serviceEntity.getSelectCount()));
        holder.tvSelectCount.setVisibility(serviceEntity.getSelectCount() == 0 ? View.GONE : View.VISIBLE);
        holder.flRoot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onServeCardClick(position, serviceEntity);

                }
            }
        });

    }

    public void setData(List<ServiceEntity> mData) {
        this.mData = mData;
    }

    @Override
    public int getItemCount() {
        return ListUtil.isListEmpty(mData) ? 0 : mData.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_service_name)
        TextView tvServiceName;
        @BindView(R.id.tv_card_tag)
        TextView tvCardTag;
        @BindView(R.id.tv_service_time)
        TextView tvServiceTime;
        @BindView(R.id.tv_service_price)
        TextView tvServicePrice;
        @BindView(R.id.tv_select_count)
        TextView tvSelectCount;
        @BindView(R.id.fl_root)
        FrameLayout flRoot;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public void setAddServeListener(AddServeListener listener) {
        this.listener = listener;
    }

    public List<ServiceEntity> getData() {
        return mData;
    }

    public interface AddServeListener {
        void onServeCardClick(int position, ServiceEntity entity);
    }
}
